# -*- coding: utf-8 -*-
import scrapy
from weather.items import WeatherItem


class HefeiWeatherSpider(scrapy.Spider):
    name = "hefei_weather"
    allowed_domains = ["localhost:8888"]
    start_urls = ['http://localhost:8888/']

    def parse(self, response):
        sub_selector = response.xpath('//ul[@class="t clearfix"]//li')
        items = []
        for sub in sub_selector:
            item = WeatherItem()
            # print '----------------------------------------'
            week = sub.xpath('./h1/text()').extract()[0]
            weather = sub.xpath('./p[1]/text()').extract()[0]
            temperature = []
            temperature.append(sub.xpath('./p[2]/span/text()').extract()[0])
            temperature.append(sub.xpath('./p[2]/i/text()').extract()[0])
            wind = sub.xpath('./p[3]/i/text()').extract()[0]

            # print week.encode('utf8')
            # print weather.encode('utf8')
            # print temperature[0].encode('utf8')
            # print temperature[1].encode('utf8')
            # print wind.encode('utf8')

            item['week'] = week
            item['weather'] = weather
            item['temperature'] = temperature
            item['wind'] = wind

            items.append(item)

        return items
